package com.example.administrator.damaiwang.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.administrator.damaiwang.Activity.App;
import com.example.administrator.damaiwang.Activity.DetailsActivity;
import com.example.administrator.damaiwang.R;
import com.example.administrator.damaiwang.Utils.HttpUtils;
import com.example.administrator.damaiwang.Utils.NetUtils;
import com.example.administrator.damaiwang.adapter.MyListAdapter;
import com.example.administrator.damaiwang.bean.LBean;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class Fragment_all extends Fragment {
    private String url;
    private ListView lv;
    private MyListAdapter adapter;
    private boolean isBottom=false;
    private LinearLayout ll;
    private static List<LBean> lBeens=new ArrayList<>();
    private TextView empty;
    private boolean isCreated=false;
    private boolean isInited=false;
    private boolean needInit=false;
    private SwipeRefreshLayout srl;
    private static sendMessage sendMessage;
    public Fragment_all(String url) {
        this.url=url;
    }
    private Handler handler=new Handler();

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (isVisibleToUser&&!isInited){
            if (isCreated){
                setContent(url);
            }
            else {
                needInit=true;
            }
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_fragment_all, container, false);
        lBeens.clear();
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        lv = (ListView) view.findViewById(R.id.lv);
        ll = (LinearLayout) view.findViewById(R.id.ll_jiazai);
        empty = (TextView) view.findViewById(R.id.empty);
        srl = (SwipeRefreshLayout) view.findViewById(R.id.srl);
        srl.setRefreshing(true);
        if (needInit){
            setContent(url);
        }
        srl.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                lBeens.clear();
                notifyChanged(url);
                srl.setRefreshing(false);
            }
        });
        lv.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
                if (scrollState==SCROLL_STATE_IDLE){
                    if (isBottom){
                        notifyChanged(url);
                    }
                    isBottom=false;
                }
            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
                if (totalItemCount!=visibleItemCount) {
                    if (firstVisibleItem + visibleItemCount == totalItemCount) {
                        isBottom = true;
                        ll.setVisibility(View.VISIBLE);
                    } else {
                        isBottom = false;
                        ll.setVisibility(View.GONE);
                    }
                }
            }
        });

        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent =new Intent(getActivity(), DetailsActivity.class);
                intent.putExtra("position",position);
                startActivity(intent);
            }
        });
        isCreated=true;
    }

    public static void send(sendMessage sendMessage){
       Fragment_all.sendMessage =sendMessage;
        sendMessage.send(lBeens);
    }
    private void notifyChanged(final String url) {
        if (!TextUtils.isEmpty(url)){
            new Thread(new Runnable() {
                @Override
                public void run() {
                    if (NetUtils.isConnect()){
                        String json = HttpUtils.getJson(url);
                        if (HttpUtils.parseJson(json) != null) {
                            lBeens.addAll(HttpUtils.parseJson(json));
                            handler.post(new Runnable() {
                                @Override
                                public void run() {
                                    adapter.notifyDataSetChanged();
                                    ll.setVisibility(View.GONE);
                                }
                            });
                        }else{
                            handler.post(new Runnable() {
                                @Override
                                public void run() {
                                    empty.setVisibility(View.VISIBLE);

                                }
                            });
                        }
                    }else {
                        handler.post(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(App.context,"请检查网络设置",Toast.LENGTH_SHORT).show();

                            }
                        });
                    }
                }
            }).start();
        }
    }

    private void setContent(final String url) {
        if (!TextUtils.isEmpty(url)){
            new Thread(new Runnable() {

                @Override
                public void run() {
                    if (NetUtils.isConnect()){
                        String json = HttpUtils.getJson(url);
                        if (HttpUtils.parseJson(json) != null) {
                            lBeens.addAll(HttpUtils.parseJson(json));
                            handler.post(new Runnable() {
                                @Override
                                public void run() {
                                    adapter = new MyListAdapter(getActivity(), lBeens);
                                    adapter.notifyDataSetChanged();
                                    lv.setAdapter(adapter);
                                }
                            });
                        }else{
                            handler.post(new Runnable() {
                                @Override
                                public void run() {
                                    empty.setVisibility(View.VISIBLE);
                                }
                            });
                        }
                    }else {
                        handler.post(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(App.context,"请检查网络设置",Toast.LENGTH_SHORT).show();

                            }
                        });
                    }
                }
            }).start();
        }
        isInited=true;
        srl.setRefreshing(false);
    }
    interface sendMessage{
        public void send(List<LBean> lBeens);
    }


}
